#include "Sock.hpp"
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>

//构建一个重定向报文返回给浏览器
void *HandlerHttpRequest(void *args)
{
    int fd = *(int *)args;
    delete (int *)args;
    pthread_detach(pthread_self());

//读取浏览器发送的请求并打印
#define SIZE 1024 * 10
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));
    ssize_t s = recv(fd, buffer, sizeof(buffer), 0);
    if (s > 0)
    {
        buffer[s] = 0;
        std::cout << buffer << std::endl; //服务端原封不动的打印客户端发送过来的内容
        //构建响应报文
        string http_response = "http/1.1 301 重定向\n";
        http_response+="Location: https://www.qq.com/\n";
        http_response+="\n";

        //给浏览器发送响应报文
        send(fd, http_response.c_str(), http_response.size(), 0);
        
    }
    close(fd); //服务端处理浏览器的请求结束后，就关闭链接，这是短服务
    return (void *)0;
}
/*
#define WWWROOT "./wwwroot/"   //这是web根目录
#define HOME_PAGE "index.html" //客户端以/路径请求时候，服务端返回的默认首页

//无论浏览器发任何请求，服务端都是响应一个首页默认网页过去
void *HandlerHttpRequest(void *args)
{
    int fd = *(int *)args;
    delete (int *)args;
    pthread_detach(pthread_self());

//读取浏览器发送的请求并打印
#define SIZE 1024 * 10
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));
    ssize_t s = recv(fd, buffer, sizeof(buffer), 0);
    if (s > 0)
    {
        buffer[s] = 0;
        std::cout << buffer << std::endl; //服务端原封不动的打印客户端发送过来的内容

        //服务器响应给客户端的html文件：html_file-->(对于http报文来说：它属于HTTP的正文部分)
        std::string html_file = WWWROOT;
        html_file += HOME_PAGE;
        //获取html文件的大小
        struct stat sta;
        stat(html_file.c_str(), &sta); //获取html文件大小，保存在sta.st_size里

        //构建响应报文
        string http_response = "http/1.0 200 Ok\n";
        http_response += "Content-Type: text/html; charset=utf8\n"; //响应正文的是html
        http_response += "Content-Length: ";
        http_response += std::to_string(sta.st_size);
        http_response += "\n";
        http_response += "\n";

        //打开html文件，为了是读取文件的内容，构建响应报文
        std::ifstream in(html_file);
        if (!in.is_open())
        {
            std::cerr << "open Error: " << html_file << std::endl;
        }
        else
        { //打开成功那么就读取文件内容
            std::string content;
            std::string line; //
            while (getline(in, line))
            {
                content += line;
            }
            //响应报文的正文构建到响应报文中
            http_response += content;
            in.close(); //关闭打开的html文件

            //给浏览器发送响应报文
            send(fd, http_response.c_str(), http_response.size(), 0);
        }
    }
    close(fd); //服务端处理浏览器的请求结束后，就关闭链接，这是短服务
    return (void *)0;
}
*/
/*
void *HandlerHttpRequest(void *args)
{
    int fd = *(int *)args;
    delete (int *)args;
    pthread_detach(pthread_self());

//读取浏览器发送的请求并打印
#define SIZE 1024 * 10
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));
    ssize_t s = recv(fd, buffer, sizeof(buffer),0);
    if(s > 0)
    {
        buffer[s] = 0;
        std::cout<<buffer<<std::endl; //服务端原封不动的打印客户端发送过来的内容


        //构建一个HTTP响应发送一个hello world给浏览器
        //但是不可以直接发送hello world 而是需要构建响应报文格式发送回去
        string http_response = "http/1.0 200 Ok\n";
        http_response+="Content-Type: text/plain\n";
        http_response+="\n";
        http_response+="hello world!";
        //发送一个hello world给浏览器
        send(fd, http_response.c_str(), http_response.size(),0);
        cout<<"发送响应完毕...\n";

    }
    close(fd); //服务端处理浏览器的请求结束后，就关闭链接，这是短服务
    return (void *)0;
}
*/
int main(int argc, char **argv)
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << "port" << endl;
    }
    cout << "1...";
    uint16_t port = atoi(argv[1]);
    int sock = Sock::Socket();
    Sock::Bind(sock, port);
    Sock::Listen(sock);
    cout << "正在监听。。。" << endl;
    for (;;)
    {
        int fd = Sock::Accept(sock);
        cout << "get a new linking...." << endl;
        if (fd > 0)
        {
            pthread_t tid;
            int *parm = new int(fd);
            pthread_create(&tid, NULL, HandlerHttpRequest, (void *)parm);
        }
    }

    return 0;
}
